import org.dom4j.DocumentException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;

/**
 * Created by Enzo Cotter on 2022/6/17.
 */
public class xmlToString {


//    public static void main(String[] args) throws IOException, JDOMException {
//        // String 转 xml
//        String xmlText = "<root><child><name>张三</name><addr></addr></child></root>";
//        StringReader reader = new StringReader(xmlText);
//        SAXBuilder saxBuilder = new SAXBuilder();
//        Document build = saxBuilder.build(reader); // 转xml
//        Element element = build.getRootElement(); // 获取到root 节点（xml最外围节点）
//        Element child = element.getChild("child"); // 获取 root 的 子节点
//        String name = child.getChildText("name");  // 获取节点的值
//        System.out.println("nane -->" + name);
//        String addr = child.getChildText("addr");
//        if (addr.isEmpty()) { // 对xml做处理，如addr为空可以添加值
//            Element addr1 = child.getChild("addr"); // 获取addr 节点 添加值
//            addr1.setText("中国");
//        }
//
//        // xml 转 String
//        XMLOutputter xmlout = new XMLOutputter();
//        ByteArrayOutputStream bo = new ByteArrayOutputStream();
//        xmlout.output(build, bo); // 把Document 写入 ByteArrayOutputStream 中
//        String xmlStr = bo.toString();
//        System.out.println(xmlStr);
//
//
//    }

    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException, DocumentException {

        File fileinput = new File("D:\\数据解析\\免疫定量分析仪_中元生物_Q7（通过解析xml文件对接）\\免疫定量分析仪_中元生物_Q7（通过解析xml文件对接）\\1最新xml文件\\2022-05-07_10_19_22.XML");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fileinput);

        // 方法1：将xml文件转化为String
//        StringWriter sw = new StringWriter();
//        TransformerFactory tf = TransformerFactory.newInstance();
//        Transformer transformer = tf.newTransformer();
//        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
//        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
//        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
//        transformer.transform(new DOMSource(doc), new StreamResult(sw));
//        System.out.println(sw);


        //方法2
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        // 将转换过的xml的String 样式打印到控制台
//        System.out.println(writer.toString());

        System.out.println(until.xml2Json(writer.toString()));

    }


//    public static void main(String[] args) {
//        String a = "28岁";
//
//        System.out.println("过滤出数字：" + a.replaceAll("\\s*","").replaceAll("[^(0-9)]",""));
//        System.out.println("过滤出汉字：" + a.replaceAll("\\s*","").replaceAll("[^(\u4e00-\u9fa5)]",""));
//
//
//    }

}
